﻿using System.Collections;
using System.Collections.Generic;
using FairyGUI;
/// <summary>
/// 游戏玩家
/// </summary>
public class UIGameMainPlayer : UIGamePlayer {

	GButton _chuBtn;
	GButton _tishiBtn;
	GButton _buchuBtn;

	GameCalcHelper.CalcResult _calcedTip = null;

	public UIGameMainPlayer(GComponent uiComp)
        : base(uiComp, true)
    {
		_chuBtn = uiComp.GetChild ("出牌按钮").asButton;
		_tishiBtn = uiComp.GetChild ("提示按钮").asButton;
		_buchuBtn = uiComp.GetChild ("不出按钮").asButton;

		_chuBtn.onClick.Add (()=>{
			DebugInfo.Log("出牌按钮被点击");

			ChuPai();
		});
		_tishiBtn.onClick.Add (()=>{
			DebugInfo.Log("提示按钮被点击");

			TiShi();
		});
		_buchuBtn.onClick.Add (()=>{
			DebugInfo.Log("不出按钮被点击");

			BuChu();
		});

		cards.OnCardSelected (OnCardSelected);

		buttonsVisible = false;
    }

	public bool buttonsVisible {
		get {
			return _chuBtn.visible;
		}
		set {
			_chuBtn.visible = _chuBtn.enabled 
				= _tishiBtn.visible = _tishiBtn.enabled
				= _buchuBtn.visible = _buchuBtn.enabled = value;

			if (GameDataKernel.inst.mustPlayCard) {
				_buchuBtn.visible = _buchuBtn.enabled = false;
			}
		}
	}

	void ChuPai() {
		List<int> selCards = cards.GetSelCards ();
		DebugInfo.Log ("SelCards: " + selCards.Count);

		GameCalcHelper.CalcResult cRes = _calcedTip;
		if (_calcedTip == null) {
			cRes = GameCalcHelper.CalcCards (selCards);
		}
		if (GameCalcHelper.CanPlay (cRes, GameDataKernel.inst.lastPlayedCards)) {
			DebugInfo.Log (cRes.ToString());

			buttonsVisible = false;
			//////////////////////////////////////
			/// 出牌
			Hashtable reqArgs = new Hashtable();
			reqArgs ["roomId"] = GameDataKernel.inst.roomId;
			reqArgs ["uid"] = DataKernel.inst.userInfo.uid;
			reqArgs ["type"] = cRes.type;
			reqArgs ["cards"] = cRes.cards.ToArray ();
			reqArgs ["point"] = cRes.maxCard;
			//UIRootController.inst.ShowMask (true);
			NetIOController.inst.SendMessage (
				new IOMessage (IOCmds.CMSG_PLAYER_PLAY_CARD, 
					IOStates.STATE_OK, new HashtableEx(reqArgs)));

			buttonsVisible = false;
			_calcedTip = null;
		} else {
			cards.DeSelect ();
		}
	}

	public void DeSelectCards() {
		cards.DeSelect ();
	}

	void TiShi() {
		int minCard = 0;
		int type = 0;
		int length = 0;
		if (!GameDataKernel.inst.mustPlayCard) {
			GameCalcHelper.CalcResult oRes = GameDataKernel.inst.lastPlayedCards;

			type = oRes.type;
			length = oRes.cards.Count;
			switch (oRes.type) {
			case GameEnum.CardType.TYPE_BOMB:
				minCard = oRes.maxCard % 100;
				break;
			case GameEnum.CardType.TYPE_ORDER:
				minCard = (oRes.maxCard % 100) - oRes.cards.Count + 1;
				break;
			case GameEnum.CardType.TYPE_ORDER_PAIR:
				minCard = (oRes.maxCard % 100) - (oRes.cards.Count / 2) + 1;
				length /= 2;
				break;
			case GameEnum.CardType.TYPE_PAIR:
				minCard = oRes.maxCard % 100;
				break;
			case GameEnum.CardType.TYPE_SINGLE:
				minCard = oRes.maxCard % 100;
				break;
			}
		}

		GameCalcHelper.CalcResult cRes = GameCalcHelper.CalcCards (GameDataKernel.inst.mainPlayer.cards, minCard, type, length);
		if (cRes != null) {
			DebugInfo.Log (cRes.ToString ());

			cards.DoSelect (cRes.cards);
			_calcedTip = cRes;
		} else {
			_calcedTip = null;
			BuChu ();
		}
	}

	void BuChu() {
		//////////////////////////////////////
		/// 不出
		Hashtable reqArgs = new Hashtable();
		reqArgs ["roomId"] = GameDataKernel.inst.roomId;
		reqArgs ["uid"] = DataKernel.inst.userInfo.uid;
		//UIRootController.inst.ShowMask (true);
		NetIOController.inst.SendMessage (
			new IOMessage (IOCmds.CMSG_PLAYER_PASS, 
				IOStates.STATE_OK, new HashtableEx(reqArgs)));

		buttonsVisible = false;
		cards.DeSelect ();
	}

	void OnCardSelected(bool sel) {
		if (_calcedTip != null) {
			_calcedTip = null;
		}
	}
}
